package com.coscon.service.impl;

import cn.hutool.json.JSONUtil;
import com.coscon.enums.*;
import com.coscon.mapper.CmdExecLogMapper;
import com.coscon.mapper.SysConfigMapper;
import com.coscon.model.IrcaCmd;
import com.coscon.model.db.CmdExecLog;
import com.coscon.model.db.SysConfig;
import com.coscon.service.IrcaCmdService;
import com.coscon.service.patternCommon.*;
import com.coscon.service.patternCommon.impl.mouse.MouseButtonCmdImpl;
import com.coscon.service.patternCommon.impl.mouse.MouseMoveCmdImpl;
import com.coscon.service.patternCommon.impl.mouse.MousePointCmdImpl;
import com.coscon.service.patternCommon.impl.mouse.MouseRollerCmdImpl;
import com.coscon.service.patternCommon.impl.music.*;
import com.coscon.service.patternCommon.impl.ppt.*;
import com.coscon.service.patternCommon.impl.systemos.*;
import com.coscon.service.patternCommon.impl.video.*;
import com.coscon.utils.DateUtils;
import com.coscon.utils.IpAddressUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.websocket.Session;
import java.awt.*;


@Service("ircaCmdService")
@Slf4j
public class IrcaCmdServiceImpl implements IrcaCmdService {

    @Resource
    private SysConfigMapper sysConfigMapper;

    //机器人
    private Robot rb = new Robot();
    //视频播放控制器
    private VideoRemoteControl videoRemoteControl = new VideoRemoteControl();
    //ppt播放控制器
    private PPtRemoteControl pPtRemoteControl = new PPtRemoteControl();
    //鼠标模拟器的控制器
    private MouseRemoteControl mouseRemoteControl = new MouseRemoteControl();
    //操作系统控制的控制器
    private SystemosRemoteControl systemosRemoteControl = new SystemosRemoteControl();
    //音频播放控制器
    private MusicRemoteControl musicRemoteControl = new MusicRemoteControl();


    @Resource
    private CmdExecLogMapper cmdExecLogMapper;


    /**
     * 构造函数，初始化命令执行器
     * @throws AWTException
     */
    public IrcaCmdServiceImpl() throws AWTException {
        initCommonMap();
    }


    @Override
    public boolean execCmd(IrcaCmd cmd, Session session) throws AWTException {
        boolean f = false;
        if(StringUtils.equalsIgnoreCase(cmd.getCmdType(), CMDTypeEnum.CMD_VIDEO.getKey()  )){
           f =  this.videoRemoteControl.execCmd( cmd, session);
        }else if(StringUtils.equalsIgnoreCase(cmd.getCmdType(), CMDTypeEnum.CMD_PPT.getKey()  )  ){
            f =  this.pPtRemoteControl.execCmd( cmd, session);
        }else if(StringUtils.equalsIgnoreCase(cmd.getCmdType(), CMDTypeEnum.CMD_SYSTEM_OS.getKey()  )  ){
            f =  this.systemosRemoteControl.execCmd( cmd, session);
        }else if(StringUtils.equalsIgnoreCase(cmd.getCmdType(), CMDTypeEnum.CMD_IRCA.getKey()  )  ){
            f = true;
        }else if(StringUtils.equalsIgnoreCase(cmd.getCmdType(), CMDTypeEnum.CMD_MUSIC.getKey()  )  ){
            f = this.musicRemoteControl.execCmd( cmd, session);
        }else if(StringUtils.equalsIgnoreCase(cmd.getCmdType(), CMDTypeEnum.CMD_MOUSE.getKey()  )  ){
            f = this.mouseRemoteControl.execCmd( cmd, session);
        }

        //记录命令执行日志
        SysConfig syslogCmdExecLogObj  = sysConfigMapper.getConfigByKey("syslog_cmd_exec_log");
        String syslogCmdExecLog = syslogCmdExecLogObj!=null?syslogCmdExecLogObj.getConfigVal():null;
        if(StringUtils.equalsIgnoreCase(syslogCmdExecLog,"1")){
            CMDTypeEnum cmdTypeEnum = CMDTypeEnum.getCmdTypeByKey(cmd.getCmdType());
            if(cmdTypeEnum!=null && cmdTypeEnum.getSaveCmdLog()
            ){
                CmdExecLog cmdExecLog = new CmdExecLog();
                cmdExecLog.setCmdData(JSONUtil.toJsonStr(cmd));
                cmdExecLog.setAddDateTime(DateUtils.nowStr());
                cmdExecLog.setOsType(SystemUtils.OS_NAME);
                cmdExecLog.setHostName(IpAddressUtils.getLocalHostName());
                cmdExecLog.setExecResult(f?"成功":"失败" );
                cmdExecLog.setCmdType(cmd.getCmdType());
                cmdExecLog.setCmd(cmd.getCmd());
                cmdExecLog.setSoftType(cmd.getSoftType());
                cmdExecLog.setValue(cmd.getValue());
                this.cmdExecLogMapper.insert(cmdExecLog);
            }
        }
        return f;
    }


    /**
     * 初始化命令执行器
     */
    private void initCommonMap(){
        //初始化PPT播放控制器
        pPtRemoteControl.addCommand(PptCmdEnum.PLAY_FROM_ONE_CMD,new PlayFromOneCmdImpl(rb) );
        pPtRemoteControl.addCommand(PptCmdEnum.PLAY_CUR_CMD,new PlayFromCurPageCmdImpl(rb) );
        pPtRemoteControl.addCommand(PptCmdEnum.NEXT_CMD,new NextCmdImpl(rb) );
        pPtRemoteControl.addCommand(PptCmdEnum.PREVIOUS_CMD,new PreviousCmdImpl(rb) );
        pPtRemoteControl.addCommand(PptCmdEnum.PLAY_END_CMD,new PlayEndCmdImpl(rb) );
        pPtRemoteControl.addCommand(PptCmdEnum.EXIT_PLAYER_CMD,new ExitPlayCmdImpl(rb) );
        //初始化视频播放器控制器
        videoRemoteControl.addCommand(VideoCmdEnum.MUTE_CMD,new PlayMuteCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.NEXT_CMD,new PlayNextCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.PLAY_STOP_CMD,new PlayOrStopCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.FULLSCREEN_CMD,new PlayFullScreenCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.EXIT_PLAYER_CMD,new PlayExitCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.PLAYER_SCREEN_SHOT_CMD,new PlayCutScreenCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.PLAYER_VOLUME_ADD_CMD,new PlayerVolumeAddCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.PLAYER_VOLUME_REDUCE_CMD,new PlayerVolumePeduceCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.PREVIOUS_CMD,new PlayPreviousCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.SPEED_ADD_CMD,new SpeedAddCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.SPEED_REDUCE_CMD,new SpeedReduceCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.SYSTEM_VOLUME_ADD_CMD,new SystemVolumeAddCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.SYSTEM_VOLUME_REDUCE_CMD,new SystemVolumePeduceCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.PLAYER_WIN_SIZE_HALF,new PlayerWinSizeHalfCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.PLAYER_WIN_SIZE_FIT,new PlayerWinSizeFitCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.PLAYER_SOUND_CHANEL,new ChangeSoundChannelCmdImpl(rb));
        videoRemoteControl.addCommand(VideoCmdEnum.PLAYER_CHANGE_SUB_TITLE,new ChangeSubtitleCmdImpl(rb));
        //初始化鼠标模拟器的控制器
        mouseRemoteControl.addCommand(MouseCmdEnum.MOVE_CMD,new MouseMoveCmdImpl(rb));
        mouseRemoteControl.addCommand(MouseCmdEnum.MOUSE_BUT,new MouseButtonCmdImpl(rb));
        mouseRemoteControl.addCommand(MouseCmdEnum.MOUSE_ROLLER,new MouseRollerCmdImpl(rb));
        mouseRemoteControl.addCommand(MouseCmdEnum.MOUSE_POINT_MOVE,new MousePointCmdImpl(rb));
        //操作系统控制的控制器
        systemosRemoteControl.addCommand(SystemOsCmdEnum.LOCKOS_CMD,new SystemOsLockCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.OPEN_FILE_CMD,new SystemOsOpenFileCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.POWER_OFF_CMD,new SystemOsPowerOffCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.POWER_REBOOT_CMD,new SystemOsRebootCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.POWER_SLEEP_CMD,new SystemOsSleepCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.SCREEN_CAPTURE_CMD,new SystemOsCutScreenCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.SELECT_WIN_START_CMD,new SystemOsSelectWinStartCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.SELECT_WIN_CHANGE_CMD,new SystemOsSelectWinChangeCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.SELECT_WIN_END_CMD,new SystemOsSelectWinEndCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.HIDDEN_ALL_WIN_CMD,new HiddenAllWinCmdImpl(rb));
        systemosRemoteControl.addCommand(SystemOsCmdEnum.MUTE_CMD,new SystemMuteCmdImpl(rb));
        //初始化音频播放器控制器
        musicRemoteControl.addCommand(MusicCmdEnum.MUTE_CMD,new MusicMuteCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.NEXT_CMD,new MusicNextCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.PLAY_STOP_CMD,new MusicOrStopCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.EXIT_PLAYER_CMD,new MusicExitCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.PLAYER_VOLUME_ADD_CMD,new MusicVolumeAddCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.PLAYER_VOLUME_REDUCE_CMD,new MusicVolumePeduceCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.PREVIOUS_CMD,new MusicPreviousCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.SPEED_ADD_CMD,new MusicSpeedAddCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.SPEED_REDUCE_CMD,new MusicSpeedReduceCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.SYSTEM_VOLUME_ADD_CMD,new MusicSystemVolumeAddCmdImpl(rb));
        musicRemoteControl.addCommand(MusicCmdEnum.SYSTEM_VOLUME_REDUCE_CMD,new MusicSystemVolumePeduceCmdImpl(rb));
    }



}
